package com.isunland.managesystem.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.DepartmentContent;
import java.util.HashMap;
import java.util.Map;

/**
 * 部门多选
 * 开发者: HuXiaoDan
 * 日期: 2017/1/6 17:13
 */
public class DeptmentMultiListAdapter extends ArrayAdapter<DepartmentContent> {

  Map<String, Boolean> isCheckMap = new HashMap<>();//<orgcode,true>

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2016-03-03 10:10:01 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final LinearLayout rootView;
    public final TextView tvTypeName;
    public final CheckBox cbTypeName;

    private ViewHolder(LinearLayout rootView, TextView tvTypeName, CheckBox cbTypeName) {
      this.rootView = rootView;
      this.tvTypeName = tvTypeName;
      this.cbTypeName = cbTypeName;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView tvTypeName = (TextView) rootView.findViewById(R.id.tv_type_name);
      CheckBox cbTypeName = (CheckBox) rootView.findViewById(R.id.cb_type_name);
      return new ViewHolder(rootView, tvTypeName, cbTypeName);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_type_list, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    DepartmentContent item = getItem(position);
    //部门名称
    vh.tvTypeName.setText(item.getOrgName());

    vh.cbTypeName.setTag(item.getOrgCode());
    // 找到需要选中的条目
    if (isCheckMap != null && isCheckMap.containsKey(item.getOrgCode())) {
      vh.cbTypeName.setChecked(true);
    } else {
      vh.cbTypeName.setChecked(false);
    }
    //复选框点击事件
    vh.cbTypeName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      private String mCheckPosition;

      @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        mCheckPosition = buttonView.getTag().toString();
        if (isChecked) {
          // 将选中的放入hashmap中
          isCheckMap.put(mCheckPosition, isChecked);
        } else {
          // 取消选中的则剔除
          isCheckMap.remove(mCheckPosition);
        }
      }
    });

    return vh.rootView;
  }

  public boolean isCheckedItem(String orgcode) {
    if (isCheckMap != null && isCheckMap.containsKey(orgcode)) {
      return true;
    } else {
      return false;
    }
  }

  private LayoutInflater mInflater;

  // Constructors
  public DeptmentMultiListAdapter(Context context, DepartmentContent[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  public DeptmentMultiListAdapter(Context context, DepartmentContent[] objects,
      Map<String, Boolean> map) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.isCheckMap = map;
  }

  @Override public boolean isEmpty() {
    return false;
  }
}

